home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / java_Win / demo / Fractal / CLSFractal.class (.txt) next >
Encoding:
Java Class File  |  1995-10-12  |  4.6 KB  |  211 lines

  1. import java.applet.Applet;
  2. import java.awt.Component;
  3. import java.awt.Event;
  4. import java.awt.Graphics;
  5. import java.util.Stack;
  6.  
  7. public class CLSFractal extends Applet implements Runnable {
  8.    ContextLSystem cls;
  9.    int fractLevel = 1;
  10.    int repaintDelay = 50;
  11.    boolean incrementalUpdates;
  12.    float startAngle;
  13.    float rotAngle;
  14.    float Xmin;
  15.    float Xmax;
  16.    float Ymin;
  17.    float Ymax;
  18.    int border;
  19.    boolean normalizescaling;
  20.    Thread kicker;
  21.    String savedPath;
  22.  
  23.    public void init() {
  24.       this.cls = new ContextLSystem(this);
  25.       String s = ((Applet)this).getParameter("level");
  26.       if (s != null) {
  27.          this.fractLevel = Integer.parseInt(s);
  28.       }
  29.  
  30.       s = ((Applet)this).getParameter("incremental");
  31.       if (s != null) {
  32.          this.incrementalUpdates = s.equals("true");
  33.       }
  34.  
  35.       s = ((Applet)this).getParameter("delay");
  36.       if (s != null) {
  37.          this.repaintDelay = Integer.parseInt(s);
  38.       }
  39.  
  40.       s = ((Applet)this).getParameter("startAngle");
  41.       if (s != null) {
  42.          this.startAngle = Float.valueOf(s);
  43.       }
  44.  
  45.       s = ((Applet)this).getParameter("rotAngle");
  46.       if (s != null) {
  47.          this.rotAngle = Float.valueOf(s);
  48.       }
  49.  
  50.       this.rotAngle = this.rotAngle / 360.0F * 2.0F * (float)Math.PI;
  51.       s = ((Applet)this).getParameter("border");
  52.       if (s != null) {
  53.          this.border = Integer.parseInt(s);
  54.       }
  55.  
  56.       s = ((Applet)this).getParameter("normalizescale");
  57.       if (s != null) {
  58.          this.normalizescaling = s.equals("true");
  59.       }
  60.  
  61.    }
  62.  
  63.    public void run() {
  64.       Thread me = Thread.currentThread();
  65.       boolean needsRepaint = false;
  66.  
  67.       while(this.kicker == me && this.cls.getLevel() < this.fractLevel) {
  68.          this.cls.generate();
  69.          if (this.kicker == me && this.incrementalUpdates) {
  70.             ((Component)this).repaint();
  71.  
  72.             try {
  73.                Thread.sleep((long)this.repaintDelay);
  74.             } catch (InterruptedException var3) {
  75.             }
  76.          } else {
  77.             needsRepaint = true;
  78.          }
  79.       }
  80.  
  81.       if (this.kicker == me) {
  82.          this.kicker = null;
  83.          if (needsRepaint) {
  84.             ((Component)this).repaint();
  85.          }
  86.       }
  87.  
  88.    }
  89.  
  90.    public void start() {
  91.       this.kicker = new Thread(this);
  92.       this.kicker.start();
  93.    }
  94.  
  95.    public void stop() {
  96.       this.kicker = null;
  97.    }
  98.  
  99.    public boolean mouseUp(Event evt, int x, int y) {
  100.       this.cls = new ContextLSystem(this);
  101.       this.savedPath = null;
  102.       this.start();
  103.       return true;
  104.    }
  105.  
  106.    public void paint(Graphics g) {
  107.       String fractalPath = this.cls.getPath();
  108.       if (fractalPath == null) {
  109.          super.paint(g);
  110.       } else {
  111.          if (this.savedPath == null || !this.savedPath.equals(fractalPath)) {
  112.             this.savedPath = fractalPath;
  113.             this.render((Graphics)null, fractalPath);
  114.          }
  115.  
  116.          for(int i = 0; i < this.border; ++i) {
  117.             g.draw3DRect(i, i, ((Component)this).size().width - i * 2, ((Component)this).size().height - i * 2, false);
  118.          }
  119.  
  120.          this.render(g, fractalPath);
  121.       }
  122.    }
  123.  
  124.    void render(Graphics g, String path) {
  125.       Stack turtleStack = new Stack();
  126.       CLSTurtle turtle;
  127.       if (g == null) {
  128.          this.Xmin = 1.0E20F;
  129.          this.Ymin = 1.0E20F;
  130.          this.Xmax = -1.0E20F;
  131.          this.Ymax = -1.0E20F;
  132.          turtle = new CLSTurtle(this.startAngle, 0.0F, 0.0F, 0, 0, 1.0F, 1.0F);
  133.       } else {
  134.          float frwidth = this.Xmax - this.Xmin;
  135.          if (frwidth == 0.0F) {
  136.             frwidth = 1.0F;
  137.          }
  138.  
  139.          float frheight = this.Ymax - this.Ymin;
  140.          if (frheight == 0.0F) {
  141.             frheight = 1.0F;
  142.          }
  143.  
  144.          float xscale = (float)(((Component)this).size().width - this.border * 2 - 1) / frwidth;
  145.          float yscale = (float)(((Component)this).size().height - this.border * 2 - 1) / frheight;
  146.          int xoff = this.border;
  147.          int yoff = this.border;
  148.          if (this.normalizescaling) {
  149.             if (xscale < yscale) {
  150.                yoff = (int)((float)yoff + ((float)(((Component)this).size().height - this.border * 2) - (this.Ymax - this.Ymin) * xscale) / 2.0F);
  151.                yscale = xscale;
  152.             } else if (yscale < xscale) {
  153.                xoff = (int)((float)xoff + ((float)(((Component)this).size().width - this.border * 2) - (this.Xmax - this.Xmin) * yscale) / 2.0F);
  154.                xscale = yscale;
  155.             }
  156.          }
  157.  
  158.          turtle = new CLSTurtle(this.startAngle, -this.Xmin, -this.Ymin, xoff, yoff, xscale, yscale);
  159.       }
  160.  
  161.       for(int pos = 0; pos < path.length(); ++pos) {
  162.          switch (path.charAt(pos)) {
  163.             case ']':
  164.                turtle = (CLSTurtle)turtleStack.pop();
  165.                break;
  166.             case '[':
  167.                turtleStack.push(turtle);
  168.                turtle = new CLSTurtle(turtle);
  169.                break;
  170.             case 'F':
  171.                if (g == null) {
  172.                   this.includePt(turtle.X, turtle.Y);
  173.                   turtle.jump();
  174.                   this.includePt(turtle.X, turtle.Y);
  175.                } else {
  176.                   turtle.draw(g);
  177.                }
  178.                break;
  179.             case '-':
  180.                turtle.rotate(-this.rotAngle);
  181.                break;
  182.             case '+':
  183.                turtle.rotate(this.rotAngle);
  184.                break;
  185.             case 'f':
  186.                turtle.jump();
  187.          }
  188.       }
  189.  
  190.    }
  191.  
  192.    void includePt(float x, float y) {
  193.       if (x < this.Xmin) {
  194.          this.Xmin = x;
  195.       }
  196.  
  197.       if (x > this.Xmax) {
  198.          this.Xmax = x;
  199.       }
  200.  
  201.       if (y < this.Ymin) {
  202.          this.Ymin = y;
  203.       }
  204.  
  205.       if (y > this.Ymax) {
  206.          this.Ymax = y;
  207.       }
  208.  
  209.    }
  210. }
  211.